#--code:utf-8 -----
import fs
import fs.copy
from openpyxl import load_workbook

def memory_op_files():
    # 创建内存文件系统
    mem_fs = fs.open_fs('mem://')
    # mem_fs.makedirs('test')

    # 把磁盘中指定的文件复制到内存文件系统
    men_file_name = "excel_file_in_memory.xlsx"
    with open("test.xlsx", "rb") as file:
       mem_fs.upload(men_file_name, file)

    # 列出内存文件系统目录树
    mem_fs.tree()

    # 打开内存文件系统中的文件，作为 openpyxl 的传入文件
    with mem_fs.open(men_file_name, "rb") as mem_excel_file:
        wb = load_workbook(mem_excel_file)
        ws = wb["test_excel_file"]
        # 遍历所有行，打印具体内容
        for row in ws.iter_rows():
            for cell in row:
                print(cell.value)

    ws['B2'] = '写入新内容'
    # 修改内容后，更新excel
    with mem_fs.open(men_file_name, "wb") as file_save:
        wb.save(file_save)

    # 从内存文件系统中，复制到磁盘
    with fs.open_fs(".") as os_fs:
        fs.copy.copy_fs(mem_fs, os_fs)
    mem_fs.close()

if __name__ == '__main__':
    memory_op_files()
